layui.use(['table', 'admin', 'ax', 'form', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var func = layui.func;

    var stuManage = {
        tableId: "stuManageTable"
    };

    /**
     * 初始化表格的列
     */
    stuManage.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'leaveUserId', align: "center", hide: true, title: '学生表记录ID'},
            {field: 'leaveUserNum', align: "center", sort: false, title: '学号'},
            {field: 'leaveUserName', align: "center", sort: false, title: '姓名'},
            {field: 'leaveUserNickName', align: "center", sort: false, title: '昵称'},
            {field: 'leaveUserSex', align: "center", sort: false, title: '性别'},
            {field: 'leaveUserWx', align: "center", sort: false, title: '微信'},
            {field: 'leaveUserTel', align: "center", sort: false, title: '手机'},
            {field: 'leaveUserAddr', align: "center", sort: false, title: '地址', minWidth: 150},
            {field: 'universityName', align: "center", sort: false, title: '学校'},
            {field: 'facultyName', align: "center", sort: false, title: '院系'},
            {field: 'clbumName', align: "center", sort: false, title: '班级'},
            {field: 'stairApproval', align: "center", sort: false, title: '直属审批人'},
            {field: 'foxbaseApproval', align: "center", sort: false, title: '院级审批人'},
            {field: 'tertiaryApproval', align: "center", sort: false, title: '校级审批人'},
            {align: 'center', toolbar: '#tableBar', title: '操作',minWidth:200}
        ]];
    };

    /**
     * 点击查询按钮
     */
    stuManage.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(stuManage.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    stuManage.openAddDlg = function () {
        func.open({
            type: 1,
            height: 1200,
            title: '添加学生信息',
            content: Feng.ctxPath + '/stuManage/add',
            tableId: stuManage.tableId
        });
    };

    /**
     * 点击编辑
     * @param data 点击按钮时候的行数据
     */
    stuManage.openEditDlg = function (data) {
        func.open({
            height: 600,
            title: '修改学生信息',
            content: Feng.ctxPath + '/stuManage/stuManage_update?leaveUserId=' + data.leaveUserId,
            tableId: stuManage.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    stuManage.exportExcel = function () {
        var checkRows = table.checkStatus(stuManage.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     */
    stuManage.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/stuManage/delete", function (data) {
                if (data.code === 200) {
                    Feng.success("删除成功!");
                    table.reload(stuManage.tableId);
                } else {
                    Feng.error("删除失败!" + data.message + "!");
                }
            });
            ajax.set("id", data.leaveUserId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + stuManage.tableId,
        url: Feng.ctxPath + '/stuManage/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: stuManage.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        stuManage.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        stuManage.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        stuManage.exportExcel();
    });


    // 工具条点击事件
    table.on('tool(' + stuManage.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            stuManage.openEditDlg(data);
        } else if (layEvent === 'delete') {
            stuManage.onDeleteItem(data);
        } else if(layEvent === 'download'){
            stuManage.onDownload(data);
        }
    });
});
